#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

constexpr int N = 1e6 + 5;

int n, ecnt;
int heads[N], dep[N], son[N], pool[N], ans[N];
int *f[N];

struct Edge {
  int v, nxt;
} e[N * 2];

inline void add(int u, int v) {
  e[++ecnt].v = v, e[ecnt].nxt = heads[u], heads[u] = ecnt;
}

void dfs1(int u, int fa) {
  for (int i = heads[u], v; i; i = e[i].nxt) {
    if ((v = e[i].v) == fa) continue;
    dfs1(v, u);
    if (dep[v] > dep[son[u]]) son[u] = v;
  }
  dep[u] = dep[son[u]] + 1;
}

inline int *allocate(int size) {
  static int *curptr = pool;
  int *res = curptr;
  curptr += size;
  return res;
}

void dfs2(int u, int fa) {
  f[u][0] = 1;
  if (son[u]) {
    f[son[u]] = f[u] + 1;
    dfs2(son[u], u);
    ans[u] = ans[son[u]] + 1;
  }
  for (int i = heads[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v == son[u] || v == fa) continue;
    f[v] = allocate(dep[v]);
    dfs2(v, u);
    for (int j = 1; j <= dep[v]; ++j) {
      f[u][j] += f[v][j - 1];
      if (f[u][j] > f[u][ans[u]] || (f[u][j] == f[u][ans[u]] && j < ans[u])) ans[u] = j;
    }
  }
  if (f[u][ans[u]] == 1) ans[u] = 0;
}

inline int main() {
  cin >> n;
  for (int i = 1, u, v; i < n; ++i) {
    cin >> u >> v;
    add(u, v), add(v, u);
  }
  dfs1(1, 0);
  f[1] = allocate(dep[1]);
  dfs2(1, 0);
  for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("CF1009F Dominant Indices.", "r", stdin);
  freopen("CF1009F Dominant Indices.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
